package smartbooks.ui.helper;

import java.util.Arrays;
import smartbooks.exceptions.MyException;
import smartbooks.ui.util.CommonConstants;
import smartbooks.ui.util.CommonFunctions;
import smartbooksdb.hibernatebean.User;

/**
 *
 * @author Ankit
 */
public class UserHelper implements CommonConstants {
    /**
     * This method is used to validate the user panel
     * @param user
     * @param cpassword
     * @return 'true' if user panel is validated or else 'false'
     * @throws MyException
     */
    public static boolean validateUser(User user, char[] cpassword) throws MyException {
        boolean userPanelValidated = true;
        
        if(null==user.getUsername() || user.getUsername().length()==0) {
            throw new MyException(USERNAME, "Username cannot be blank");
        }
        
        if(null==user.getPassword() || user.getPassword().length()==0) {
            throw new MyException(PASSWORD, "Password cannot be blank");
        }
        
        /*
        if(null==cpassword || cpassword.length<8) {
            throw new MyExceptionEarlier(PASSWORD, "Confirm Password should have minimum 8 characters");
        }
        * 
        */
        
        if(!Arrays.equals(user.getPassword().toCharArray(), cpassword)) {
            throw new MyException(PASSWORD, "Passwords mismatch");
        }
        
        if(!user.getRole().equals(USER_ROLE_ADMIN) && !user.getRole().equals(USER_ROLE_EMPLOYEE)) {
            throw new MyException(ROLE, "Please select a Role");
        }
        
        if(null==user.getFirstName() || user.getFirstName().length()==0) {
            throw new MyException(FIRSTNAME, "Firstname cannot be blank");
        }
        
        if(null!=user.getState() && user.getState().length()>32) {
            throw new MyException(STATE, "State should have maximum 32 characters");
        }
        
        if(null!=user.getCity() && user.getCity().length()>32) {
            throw new MyException(CITY, "City should have maximum 32 characters");
        }
        
        if(!user.getGender().equals(GENDER_MALE) && !user.getGender().equals(GENDER_FEMALE)) {
            throw new MyException(GENDER, "Please select a Gender");
        }
        
        if(null!=user.getEmail() && !CommonFunctions.validateEmail(user.getEmail())) {
            throw new MyException(EMAIL, "Invalid Email");
        }
        
        if(null!=user.getPanNumber() && user.getPanNumber().length()>0 && user.getPanNumber().length()!=10) {
            throw new MyException(PAN, "PAN Number should have 10 characters");
        }
        
        return userPanelValidated;
    }
}
